package com.zw.weiyi.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zw.weiyi.enety.Call;
import com.zw.weiyi.phonesms.R;
import com.zw.weiyi.util.PhoneUtil;

/**
 * 通话记录适配器 
 * @author Striver_zw 
 * Date 2014-12-9
 */
public class CallAdapter extends BaseAdapter{

	private Context context;
	private List<Call> callList;
	private LayoutInflater inflater;
	

	public CallAdapter(Context context, List<Call> calls) {
		this.context = context;
		this.callList = calls;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return callList.size();
	}

	@Override
	public Object getItem(int position) {
		return callList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.call_fragment_item, null);
			holder = new ViewHolder();
			holder.call_type = (ImageView) convertView
					.findViewById(R.id.call_type);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.number = (TextView) convertView.findViewById(R.id.number);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.call_btn = (TextView) convertView
					.findViewById(R.id.call_btn);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Call call = callList.get(position);
		switch (call.getType()) {
		case 1:
			holder.call_type// 判断电话状态
					.setBackgroundResource(R.drawable.ic_calllog_outgoing_nomal);
			break;
		case 2:
			holder.call_type
					.setBackgroundResource(R.drawable.ic_calllog_incomming_normal);
			break;
		case 3:
			holder.call_type
					.setBackgroundResource(R.drawable.ic_calllog_missed_normal);
			break;
		}
		holder.name.setText(call.getName());
		holder.number.setText(call.getNumber());
		holder.time.setText(call.getDate());
		addViewListener(holder.call_btn, call, position);

		return convertView;
	}

	private static class ViewHolder {
		ImageView call_type;
		TextView name;
		TextView number;
		TextView time;
		TextView call_btn;
	}

	// 点击打击电话
	private void addViewListener(View view, final Call callLog,
			final int position) {
		view.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				PhoneUtil.phoneNumber(context, callLog.getNumber());				
			}
		});
	}
}
